﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    /// <summary>
    /// A SpriteSheet animation Component.
    /// </summary>
    public class Animation : Behaviour
    {
        private Single mTimer = 0f;
        private Single mInterval = 1000f / 25f;
        private Int32 mFrameCount = 16;
        private Int32 mCurrentFrame = 0;
        private Vector2 mFrameSize = new Vector2(64, 64);
        private Renderer mRenderer;

        /// <summary>
        /// The Renderer component this Animation is using.
        /// </summary>
        public Renderer AnimationRenderer { get { return mRenderer; } }

        /// <summary>
        /// The sprite sheet Texture to use.
        /// </summary>
        public Texture2D Texture { get { return mRenderer.Texture; } set { mRenderer.Texture = value; } }

        /// <summary>
        /// The amount of time each frame should be visible for.
        /// </summary>
        public Single Interval { get { return mInterval; } set { mInterval = value; } }

        /// <summary>
        /// The amount of frames in this Animation.
        /// </summary>
        public Int32 FrameCount { get { return mFrameCount; } set { mFrameCount = value; } }

        /// <summary>
        /// The frame the Animation is currently on.
        /// </summary>
        public Int32 CurrentFrame { get { return mCurrentFrame; } set { mCurrentFrame = value; } }

        /// <summary>
        /// The size of each frame in the Animation.
        /// </summary>
        public Vector2 FrameSize { get { return mFrameSize; } set { mFrameSize = value; } }

        /// <summary>
        /// Is this Animation currently visible?
        /// </summary>
        public Boolean Visible { get { return mRenderer.Visible; } set { mRenderer.Visible = value; } }

        public Animation(GameObject gameObject)
            : base(gameObject)
        {
            mRenderer = gameObject.AddComponent<Renderer>(Name);
        }

        /// <summary>
        /// Updates this Animation.
        /// </summary>
        public override void Update()
        {
            if (Texture != null)
            {
                mRenderer.DestinationRectangle = new Rectangle((Int32)GameObject.Transform.Position.X, (Int32)GameObject.Transform.Position.Y, (Int32)mFrameSize.X * (Int32)GameObject.Transform.Scale.X, (Int32)mFrameSize.Y * (Int32)GameObject.Transform.Scale.Y);
                mRenderer.Origin = mFrameSize / 2;
            }

            mTimer += TimeManager.Instance.Delta;

            if (mTimer > mInterval)
            {
                mCurrentFrame++;

                if (mCurrentFrame > mFrameCount - 1)
                {
                    mCurrentFrame = 0;
                }

                mTimer = 0f;
            }

            mRenderer.SourceRectangle = new Rectangle((Int32)mCurrentFrame * (Int32)mFrameSize.X, 0, (Int32)mFrameSize.X, (Int32)mFrameSize.Y);
        }
    }
}
